package com.ssy.lingxi.message.websocket.model;

import lombok.Getter;
import lombok.ToString;

import java.io.Serializable;

/**
 * webSocket消息实体
 * @author yzc
 * @version 2.0.0
 * @date 2021/5/25
 */
@ToString
@Getter
public class WsMessage implements Serializable {

    private static final long serialVersionUID = 1684345391934391870L;

    private WsMessage() {
    }

    public WsMessage(String sender, String receiver, String action, Object data) {
        this.sender = sender;
        this.receiver = receiver;
        this.action = action;
        this.data = data;
    }

    /**
     * 发送者标识
     */
    private String sender;
    /**
     * 接收者标识
     */
    private String receiver;
    /**
     * 消息类型 WsAction
     */
    private String action;
    /**
     * 消息内容
     */
    private Object data;


    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public static WebSocketMessageBuilder builder() {
        return new WebSocketMessageBuilder();
    }

    public static class WebSocketMessageBuilder {
        private String sender = "0";
        private String receiver;
        private String action;
        private Object data;

        public WebSocketMessageBuilder receiver(String receiver) {
            this.receiver = receiver;
            return this;
        }

        public WebSocketMessageBuilder action(String action) {
            this.action = action;
            return this;
        }

        public WebSocketMessageBuilder data(Object data) {
            this.data = data;
            return this;
        }


        public WsMessage build() {
            return new WsMessage(this.sender, this.receiver, this.action, this.data);
        }
    }
}
